package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.Constants;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.MUserExcution;
import com.fortune.dto.in.mUser.ChangePasswordDto;
import com.fortune.dto.in.mUser.LoginDto;
import com.fortune.dto.in.mUser.ResetPasswordDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MUserEnum;
import com.fortune.service.MUserService;
import com.fortune.vo.mUser.LoginMUserInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(value = "muser", description = "厂商员工账号相关", tags = {"厂商员工账号相关"})
@RestController
@RequestMapping("mUser")
public class MUserController{
	
	@Autowired
	private MUserService mUserService;
	@Autowired
	private RedisClient redisClient;
	
	@ApiOperation(value = "厂商员工登录", nickname = "mUserLogin", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "login",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<LoginMUserInfo> login(HttpServletRequest request,@Validated @RequestBody LoginDto loginDto) {
			int check = mUserService.loginCheck(loginDto);
			if (check==1) {
				return new MUserExcution<LoginMUserInfo>(null, MUserEnum.PASSWORDFALSE);
			}else if (check==2) {
				return new MUserExcution<LoginMUserInfo>(null, MUserEnum.PHONEFALSE);
			}else {
				MUserInfo mUserInfo = mUserService.mUserInfo(loginDto);
				HttpSession session = request.getSession();
				session.setAttribute(Constants.SESSION_M_USER, mUserInfo);
				LoginMUserInfo loginMUserInfo = mUserService.loginMUserInfo(loginDto);
				return new MUserExcution<LoginMUserInfo>(loginMUserInfo, MUserEnum.SUCCESS);
			}
	}
	
	@ApiOperation(value = "厂商员工退出登录", nickname = "mUserLogout", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "logout")
	public BaseAPIExcution<Void> logount(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute(Constants.SESSION_M_USER);
		return new MUserExcution<Void>(null,MUserEnum.SUCCESS);
	}
	
	@ApiOperation(value = "厂商员工重置密码", nickname = "mUserResetPassword", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "password/reset",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(HttpServletRequest request,@Validated @RequestBody ResetPasswordDto resetPasswordDto) {
		redisClient.save("123456", 5, CacheKeys.SALES_MAN_LOGIN_TOKEN, "fda");
		String check = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, "fda");
		if (check==null || check.isEmpty()) {
			return new MUserExcution<Void>(null, MUserEnum.SESSIONCODEFALSE);
		}
		if (check.equals(resetPasswordDto.getCode())) {
			HttpSession session = request.getSession();
			MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
			if (mUserInfo==null) {
				return new MUserExcution<Void>(null, MUserEnum.NOLOGIN);
			}
			int result = mUserService.resetPassword(mUserInfo, resetPasswordDto);
			if (result==1) {
				return new MUserExcution<Void>(null, MUserEnum.SUCCESS);
			}
			return new MUserExcution<Void>(null, MUserEnum.FALSE);
		}
		return new MUserExcution<Void>(null, MUserEnum.CODEFALSE);
	}
	
	@ApiOperation(value = "厂商员工修改密码", nickname = "mUserChangePassword", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "password/change",
    	produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> changePassword(HttpServletRequest request,@Validated @RequestBody ChangePasswordDto changePasswordDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		if (mUserInfo==null) {
			return new MUserExcution<Void>(null, MUserEnum.NOLOGIN);
		}
		int result =mUserService.updatePassword(mUserInfo, changePasswordDto);
		if (result==0) {
			return new MUserExcution<Void>(null, MUserEnum.PASSWORDFALSE);
		}
		return new MUserExcution<Void>(null, MUserEnum.SUCCESS);
	}

}
